package com.hkts.naturalgas.datainterface.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import com.hkts.naturalgas.datahandler.configurationfile.IpWhiteList;
import com.hkts.naturalgas.datahandler.curvecache.CurveCache;
import com.hkts.naturalgas.datahandler.entity.FunctionCache;
import com.hkts.naturalgas.dictionaries.service.IHktsEnvironmentProtectionItemService;
import com.hkts.naturalgas.login.config.CheckUrls;
import com.hkts.naturalgas.tcp.SummaryConstance;
import com.hkts.naturalgas.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassNameTestController
 * @Description
 * @Author yck
 * @Date2020-07-27 11:27
 * @Version V1.0
 **/
@Slf4j
@RestController
public class DataInterfaceController {
@Autowired
private HttpDataScheduling httpDataScheduling;
  @Resource
  private HttpAPIService httpAPIService;
  @Autowired
  public IHktsEnvironmentProtectionItemService iHktsEnvironmentProtectionItemService;

  @Autowired
  private IpWhiteList ipWhiteList;
  /** 功能描述:
   *[]
   * @return: com.hkts.naturalgas.utils.Result
   * @Author: 杨春科
   * @Date: 2021-11-02 16:30
   * @Description //TODO 市局测试数据接口
   */


  //顺满满每日数据
  @GetMapping("/hbhkts/dataSummary")
  public Map<String, Object> dataSummary()throws Exception{
    Map<String, Object> map = DataSummary.getCargoAndOrder();
    Map<String, Object> userInfo = DataSummary.getUserInfo();
    map.putAll(userInfo);
    return map;
  }
/** 功能描述:
*[]
* @return: com.hkts.naturalgas.utils.Result
* @Author: 杨春科
* @Date: 2021-11-02 16:30
* @Description // 市环保局使用接口
*/
  @GetMapping("/hbhkts/dataInterface")
  public Result dataInterface(HttpServletRequest request)throws Exception{
    List<String> whiteList = ipWhiteList.getWhiteList();
    String ipAddress = null;
    String[] header = {"x-forwarded-for","Proxy-Client-IP","WL-Proxy-Client-IP"};
    for (int i = 0; i < header.length; i++) {
      ipAddress = request.getHeader(header[i]);
      if (StringUtils.isEmpty(ipAddress) || "unknown".equalsIgnoreCase(ipAddress)) {
        continue;
      } else {
        break;
      }
    }
    if (StringUtils.isEmpty(ipAddress) || "unknown".equalsIgnoreCase(ipAddress)) {
      ipAddress = request.getRemoteAddr();
      if (ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")) {
        // 根据网卡取本机配置的IP
        InetAddress inet = null;
        try {
          inet = InetAddress.getLocalHost();
        } catch (Exception e) {
          e.printStackTrace();
        }
        ipAddress = inet.getHostAddress();
      }
    }
    // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
    // "***.***.***.***".length() = 15
    if (!StringUtils.isEmpty(ipAddress) && ipAddress.length() > 15) {
      if (ipAddress.indexOf(",") > 0) {
        ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
      }
    } else if (StringUtils.isEmpty(ipAddress)) {
      throw new Exception("获取ip异常");
    }
   log.info("市局接口请求ip{}",ipAddress);
  if(whiteList.contains(ipAddress)){
   // Map<String, Object> map = iHktsEnvironmentProtectionItemService.dataInterface();
    Map<String, Object> map = httpDataScheduling.HttpData();
    return ResultUtil.data(map);
  }
  //  Map<String, Object> map = httpDataScheduling.HttpData();
    return ResultUtil.error("请和网站管理员确认请求ip是否有效！");
  }
  /** 功能描述:
  *[]
  * @return: com.hkts.naturalgas.utils.Result
  * @Author: 杨春科
  * @Date: 2021-11-02 16:30
  * @Description //发改委使用接口
  */
  @GetMapping("/hbhkts/powerDataInterface")
  public Result powerDataInterface()throws Exception{
     //Map<String, Object> map = iHktsEnvironmentProtectionItemService.powerDataInterface();
    Map<String, Object> map = httpDataScheduling.powerHttpData();
    return ResultUtil.data(map);
  }

  /** 功能描述:
   *[]
   * @return: com.hkts.naturalgas.utils.Result
   * @Author: 杨春科
   * @Date: 2021-11-02 16:30
   * @Description //白晶公司使用的接口
   */
  @GetMapping("/hbhkts/baijingDataInterface")
  public Result baijingDataInterface()throws Exception{
    //Map<String, Object> map = httpDataScheduling.baijingDataInterface();
   Map<String, Object> map = iHktsEnvironmentProtectionItemService.baijingDataInterface();
    return ResultUtil.data(map);
  }
  /** 功能描述:
  *[]
  * @return: com.hkts.naturalgas.utils.Result
  * @Author: 杨春科
  * @Date: 28/12/2021 上午10:03
  * @Description //
  */
  @GetMapping("/hbhkts/totalData")
  public Result totalData()throws Exception{
    Map<String, Object> result = new HashMap<String, Object>();
    String s= httpAPIService.doGet("http://192.168.99.67:5000//biz");
    Gson gson = new Gson();
    Map map = new HashMap();
    map = gson.fromJson(s, map.getClass());
    return ResultUtil.data(map);
  }
  @GetMapping("/hbhkts/testData")
  public Result testData(String s)throws Exception{
    if(s.equals("1")){
      s="##1299QN=20220625230535035;ST=31;CN=2061;PW=123456;MN=ZG130121201631;Flag=5;\n" +
              "CP=\\u0026\\u0026DataTime=20220625220000;\n" +
              "a00000-Cou=27534.42,a00000-Min=6.124,a00000-Avg=7.648,a00000-Max=8.348,a00000-Flag=N;\n" +
              "a01011-Min=3.027,a01011-Avg=3.798,a01011-Max=4.15,a01011-Flag=N;a01012-Min=60.806,a01012-Avg=61.193,a01012-Max=61.552,a01012-Flag=N;\n" +
              "a01013-Min=161.407,a01013-Avg=165.365,a01013-Max=169.545,a01013-Flag=N;a01014-Min=21.297,a01014-Avg=21.61,a01014-Max=21.941,a01014-Flag=N;a19001-Min=15.359,a19001-Avg=15.447,a19001-Max=15.545,a19001-Flag=N;\n" +
              "a21001-Cou=0.06,a21001-Min=1.365,a21001-Avg=1.389,a21001-Max=1.409,a21001-Flag=N;\n" +
              "a21001-ZsCou=0.06,a21001-ZsMin=1.365,a21001-ZsAvg=1.389,a21001-ZsMax=1.409,a21001-ZsFlag=N;\n" +
              "a21002-Cou=0.825987,a21002-Min=24.15623,a21002-Avg=32.178004,a21002-Max=43.360711,a21002-Flag=N;\n" +
              "a21002-ZsCou=0.825987,a21002-ZsMin=24.15623,a21002-ZsAvg=32.178004,a21002-ZsMax=43.360711,a21002-ZsFlag=N;\n" +
              "a21026-Cou=0.709354,a21026-Min=18.466331,a21026-Avg=27.632627,a21026-Max=31.933069,a21026-Flag=N;\n" +
              "a21026-ZsCou=0.709354,a21026-ZsMin=18.466331,a21026-ZsAvg=27.632627,a21026-ZsMax=31.933069,a21026-ZsFlag=N;\n" +
              "a34013-Cou=0.008032,a34013-Min=0.225157,a34013-Avg=0.337205,a34013-Max=0.555706,a34013-Flag=N;a34013-ZsCou=0.008032,a34013-ZsMin=0.225157,a34013-ZsAvg=0.337205,a34013-ZsMax=0.555706,a34013-ZsFlag=N\\u0026\\u0026DEC1\\r\\n##1295QN=20220625230435046;ST=31;CN=2051;\n" +
              "PW=123456;MN=ZG130121201631;Flag=5;CP=\\u0026\\u0026DataTime=20220625230400;\n" +
              "a00000-Cou=466.2,a00000-Min=7.328,a00000-Avg=7.77,a00000-Max=8.058,a00000-Flag=N;\n" +
              "a01011-Min=3.656,a01011-Avg=3.878,a01011-Max=4.02,a01011-Flag=N;\n" +
              "a01012-Min=61.443,a01012-Avg=61.518,a01012-Max=61.552,a01012-Flag=N;\n" +
              "a01013-Min=163.215,a01013-Avg=165.647,a01013-Max=169.093,a01013-Flag=N;\n" +
              "a01014-Min=21.912,a01014-Avg=21.934,a01014-Max=21.959,a01014-Flag=N;\n" +
              "a19001-Min=15.353,a19001-Avg=15.36,a19001-Max=15.366,a19001-Flag=N;a21001-Cou=0.001,a21001-Min=1.378,a21001-Avg=1.393,a21001-Max=1.405,a21001-Flag=N;\n" +
              "a21001-ZsCou=0.001,a21001-ZsMin=1.378,a21001-ZsAvg=1.393,a21001-ZsMax=1.405,a21001-ZsFlag=N;a21002-Cou=0.013697,a21002-Min=27.07630\\n";
    }else if(s.equals("2")){
        s="##1271QN=20220625233145459;ST=31;CN=2051;PW=123456;MN=ZG130121201631;" +
                "Flag=5;CP=\\u0026\\u0026DataTime=20220625232700;a00000-Cou=296.7,a00000-Min=4.149,a00000-Avg=4.945,a00000-Max=5.448,a00000-Flag=N;a01011-Min=2.066,a01011-Avg=2.462,a01011-Max=2.713,a01011-Flag=N;a01012-Min=61.443,a01012-Avg=61.476,a01012-Max=61.552,a01012-Flag=N;a01013-Min=136.992,a01013-Avg=138.905,a01013-Max=141.062,a01013-Flag=N;" +
                "a01014-Min=21.716,a01014-Avg=21.741,a01014-Max=21.76,a01014-Flag=N;a19001-Min=14.327,a19001-Avg=14.336,a19001-Max=14.341,a19001-Flag=N;a21001-Cou=0,a21001-Min=1.38,a21001-Avg=1.39,a21001-Max=1.407,a21001-Flag=N;a21001-ZsCou=0,a21001-ZsMin=1.38,a21001-ZsAvg=1.39,a21001-ZsMax=1.407,a21001-ZsFlag=N;" +
                "a21002-Cou=0.000441,a21002-Min=1.448848,a21002-Avg=1.48698,a21002-Max=1.517557,a21002-Flag=N;a21002-ZsCou=0.000441,a21002-ZsMin=1.448848,a21002-ZsAvg=1.48698,a21002-ZsMax=1.517557,a21002-ZsFlag=N;a21026-Cou=0.000012,a21026-Min=0.036831,a21026-Avg=0.039298,a21026-Max=0.04108,a21026-Flag=N;" +
                "a21026-ZsCou=0.000012,a21026-ZsMin=0.036831,a21026-ZsAvg=0.039298,a21026-ZsMax=0.04108,a21026-ZsFlag=N;a34013-Cou=0.000183,a34013-Min=0.56538,a34013-Avg=0.617981,a34013-Max=0.660386,a34013-Flag=N;a34013-ZsCou=0.000183,a34013-ZsMin=0.56538,a34013-ZsAvg=0.617981,a34013-ZsMax=0.660386,a34013-ZsFlag=N\\u0026\\u0026F881\\r\\n\"";
    }if(s.equals("3")){
        s="0723,a21026-ZsAvg=0.061783,a21026-ZsMax=0.062697,a21026-ZsFlag=N;a34013-Cou=0.000132,a34013-Min=0.346766,a34013-Avg=0.466477,a34013-Max=0.557719,a34013-Flag=N;a34013-ZsCou=0.000132,a34013-ZsMin=0.346766,a34013-ZsAvg=0.466477,a34013-ZsMax=0.557719,a34013-ZsFlag=N\\u0026\\u002656C0\\r\\n";
    }
    if(s.contains(SummaryConstance.BEGIN_FLAG)){
      String[] split = s.split(SummaryConstance.BEGIN_FLAG);
      for(int i=0;i<split.length;i++){
        //包含开头和结尾则进行数据处理
        if(split[i].contains(SummaryConstance.MINUTE_DATA_FLAG)&&split[i].contains(SummaryConstance.END_FLAG)){
          //handlerData(split[i]);
          log.info("数据处理：{}",split[i]);
          //包含开头不包含结尾需要处理拆包，放入全局常量等待处理
        }else if(split[i].contains(SummaryConstance.MINUTE_DATA_FLAG)&&!split[i].contains(SummaryConstance.END_FLAG)){
          SummaryConstance.CACHE_DATA=split[i];
          log.info("数据缓存：{}",SummaryConstance.CACHE_DATA);
        }
      }

    }else{
      if(StringUtils.isNotBlank(SummaryConstance.CACHE_DATA)){
        s= SummaryConstance.CACHE_DATA+s;
        SummaryConstance.CACHE_DATA="";
        log.info("数据处理：{}",s);
      }

    }
    return ResultUtil.success("提交成功");
  }
}
